<template>
  <div class="product-detail-container">
    <div class="product-detail-header">
      <router-link to="/" class="back-link">返回首页</router-link>
    </div>
    
    <div v-if="product" class="product-detail-content">
      <div class="product-image-section">
        <img :src="product.image" :alt="product.name" class="product-detail-image">
      </div>
      
      <div class="product-info-section">
        <h1 class="product-detail-title">{{ product.name }}</h1>
        <p class="product-detail-price">￥{{ product.price }}</p>
        <p class="product-detail-description">{{ product.description }}</p>
        
        <div class="product-options">
          <div class="quantity-selector">
            <label for="quantity">数量：</label>
            <input type="number" id="quantity" v-model.number="quantity" min="1" value="1">
          </div>
          
          <button class="add-to-cart-button">加入购物车</button>
          <button class="purchase-now-button" @click="purchaseNow">立即购买</button>
        </div>
        
        <div class="product-details">
          <h3>商品详情</h3>
          <p>类别：{{ categoryName }}</p>
          <p>编号：{{ product.id }}</p>
          <p>工艺：手工制作</p>
          <p>材质：优质陶瓷</p>
        </div>
      </div>
    </div>
    
    <div v-else class="loading">加载中...</div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()
const product = ref(null)
const categoryName = ref('')
const quantity = ref(1)

// 模拟产品数据获取
onMounted(() => {
  // 获取URL中的productId参数
  const productId = route.params.id
  
  // 这里应该从API获取商品详情，现在使用模拟数据
  // 实际项目中应该从store或API获取数据
  const mockProductData = {
    id: productId,
    name: '青瓷花瓶',
    description: '优雅的青瓷花瓶，采用传统工艺手工制作，釉色自然流畅，造型简洁大方。',
    price: 199,
    image: 'https://picsum.photos/600/400'
  }
  
  product.value = mockProductData
  categoryName.value = '花瓶'
})

// 立即购买处理
const purchaseNow = () => {
  ElMessage.success(`成功下单：${product.value.name}，数量：${quantity.value}`)
  // 实际项目中应该跳转到订单确认页面
  setTimeout(() => {
    router.push('/')
  }, 1500)
}
</script>

<style scoped>
.product-detail-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.product-detail-header {
  margin-bottom: 20px;
}

.back-link {
  color: #667eea;
  text-decoration: none;
  font-size: 16px;
  padding: 8px 16px;
  border: 1px solid #667eea;
  border-radius: 4px;
  transition: all 0.3s;
}

.back-link:hover {
  background-color: #667eea;
  color: white;
}

.product-detail-content {
  display: flex;
  gap: 40px;
  margin-bottom: 50px;
}

.product-image-section {
  flex: 1;
  max-width: 500px;
}

.product-detail-image {
  width: 100%;
  height: auto;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.product-info-section {
  flex: 1;
}

.product-detail-title {
  font-size: 28px;
  margin-bottom: 15px;
  color: #333;
}

.product-detail-price {
  font-size: 24px;
  color: #e74c3c;
  margin-bottom: 20px;
  font-weight: bold;
}

.product-detail-description {
  font-size: 16px;
  line-height: 1.6;
  color: #666;
  margin-bottom: 30px;
}

.product-options {
  margin-bottom: 40px;
}

.quantity-selector {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.quantity-selector input {
  width: 60px;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
}

.add-to-cart-button,
.purchase-now-button {
  padding: 12px 24px;
  font-size: 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
  margin-right: 10px;
}

.add-to-cart-button {
  background-color: #f39c12;
  color: white;
}

.add-to-cart-button:hover {
  background-color: #e67e22;
}

.purchase-now-button {
  background-color: #e74c3c;
  color: white;
}

.purchase-now-button:hover {
  background-color: #c0392b;
}

.product-details {
  border-top: 1px solid #eee;
  padding-top: 20px;
}

.product-details h3 {
  margin-bottom: 15px;
  color: #333;
}

.product-details p {
  margin-bottom: 10px;
  color: #666;
}

.loading {
  text-align: center;
  padding: 50px;
  font-size: 18px;
  color: #666;
}

@media (max-width: 768px) {
  .product-detail-content {
    flex-direction: column;
  }
  
  .product-image-section {
    max-width: 100%;
  }
  
  .add-to-cart-button,
  .purchase-now-button {
    display: block;
    width: 100%;
    margin-bottom: 10px;
  }
}
</style>